package lib.self.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * description: 时间工具类
 * author: Steven Xiao
 * date: 2017/6/15
 */
public class TimeUtil {

    /**
     * 时间转化为年月日小时分秒
     *
     * @param value
     * @return
     */
    public static String getNormalTime(long value) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(value);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 时间转换成日期
     *
     * @param value
     * @return
     */
    public static String getDate(long value) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = new Date(value);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * @param value
     * @return 小时和分
     */
    public static String getHourAndMinutes(long value) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        Date date = new Date(value);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 日期转换成年月日
     */
    public static String dateToYMD(String time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = format.parse(time);
            format = new SimpleDateFormat("yyyy年MM月dd日");
            return format.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * dateTime 转换成年月日小时分
     */
    public static String dateToYMDHMDigital(String time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            Date date = format.parse(time);
            return format.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }


    /**
     * dateTime 转换成年月日
     */
    public static String dateToYMDDigital(String time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = format.parse(time);
            format = new SimpleDateFormat("yyyy-MM-dd");
            return format.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * dateTime 转换成时间
     */
    public static String dateToHMDigital(String time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            Date date = format.parse(time);
            format = new SimpleDateFormat("HH:mm");
            return format.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 分钟数换成时分数
     */
    public static String minutesToHourAndMinutes(int time) {
        int hours = time / 60;
        if (hours == 0) {
            return time + "分";
        } else {
            int minutes = time % 60;
            return hours + "时" + minutes + "分";
        }
    }
}
